/*
 * Created by JFormDesigner on Wed Jul 07 09:50:41 CST 2021
 */

package user;

import controller.SendFile;
import model.FileModel;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * @author DarkFire
 */
public class sendPanel extends JPanel {
    private static JButton btnSendFile;
    private static JLabel textTip;

    public sendPanel() {
        initComponents();
    }


    private void btnChooseFileActionPerformed(ActionEvent e) {

            JFileChooser fcDlg = new JFileChooser();
            fcDlg.setDialogTitle("请选择需要传输的文件");

            int returnVal = fcDlg.showOpenDialog(null);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                String filepath = fcDlg.getSelectedFile().getPath();
                textPath.setText(filepath);
                File file=new File(filepath);
                float size=Float.valueOf(file.length())/1024/1024;
                textSize.setText("大小:"+size+" MB");
        }
    }

    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
    }

    private void btnSendFileActionPerformed(ActionEvent e)  {
       String ip=textIP.getText();
       String path=textPath.getText();
      btnSendFile.setEnabled(false);
       if (ip.length()<6||path.length()<6){
           JOptionPane.showMessageDialog(null,"请输入正确的IP 或 选择正确的文件路径!");
           btnSendFile.setEnabled(true);
       }else{
           textTip.setText("正在连接....");
           File file=new File(path);
           //开始封装信息
           FileModel file1=new FileModel(ip,file);
           try {
               new SendFile(file1);
           } catch (FileNotFoundException fileNotFoundException) {
               fileNotFoundException.printStackTrace();
           }
       }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        label1 = new JLabel();
        textIP = new JTextField();
        label2 = new JLabel();
        btnChooseFile = new JButton();
        textPath = new JTextField();
        btnSendFile = new JButton();
        textSize = new JLabel();
        textTip = new JLabel();

        //======== this ========
        setLayout(null);

        //======== panel1 ========
        {
            panel1.setLayout(null);

            //---- label1 ----
            label1.setText("\u8f93\u5165\u63a5\u6536\u8005IP:");
            panel1.add(label1);
            label1.setBounds(5, 5, 105, 25);
            panel1.add(textIP);
            textIP.setBounds(90, 5, 140, textIP.getPreferredSize().height);

            //---- label2 ----
            label2.setText("\u9009\u62e9\u4f20\u8f93\u7684\u6587\u4ef6:");
            panel1.add(label2);
            label2.setBounds(5, 45, 105, 25);

            //---- btnChooseFile ----
            btnChooseFile.setText("\u5f00\u59cb\u9009\u62e9");
            btnChooseFile.addActionListener(e -> {
			btnChooseFileActionPerformed(e);
		});
            panel1.add(btnChooseFile);
            btnChooseFile.setBounds(new Rectangle(new Point(120, 40), btnChooseFile.getPreferredSize()));

            //---- textPath ----
            textPath.setText("C:/user/Administrator/Desktop/");
            textPath.setEnabled(false);
            panel1.add(textPath);
            textPath.setBounds(5, 75, 330, 25);

            //---- btnSendFile ----
            btnSendFile.setText("\u5f00\u59cb\u53d1\u9001");
            btnSendFile.addActionListener(e -> {
			btnSendFileActionPerformed(e);
		});
            panel1.add(btnSendFile);
            btnSendFile.setBounds(20, 115, 305, 30);

            //---- textSize ----
            textSize.setText(" ");
            textSize.setForeground(new Color(255, 0, 51));
            panel1.add(textSize);
            textSize.setBounds(210, 45, 180, 22);

            //---- textTip ----
            textTip.setText(" ");
            textTip.setForeground(new Color(255, 0, 51));
            textTip.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
            panel1.add(textTip);
            textTip.setBounds(0, 150, 365, 30);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        add(panel1);
        panel1.setBounds(5, 5, 395, 290);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < getComponentCount(); i++) {
                Rectangle bounds = getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            setMinimumSize(preferredSize);
            setPreferredSize(preferredSize);
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JLabel label1;
    private JTextField textIP;
    private JLabel label2;
    private JButton btnChooseFile;
    private JTextField textPath;

    private JLabel textSize;

    // JFormDesigner - End of variables declaration  //GEN-END:variables
    public static JButton getBtnSendFile(){
        return btnSendFile;
    }
    public static JLabel getTextTip(){
        return textTip;
    }
}
